package com.clw.template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.clw.template.dto.Tree;
import com.clw.template.entity.SysPermission;
import com.clw.template.entity.SysRolePermissionRelation;
import com.clw.template.mapper.SysPermissionMapper;
import com.clw.template.service.ISysPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.clw.template.service.ISysRolePermissionRelationService;
import com.clw.template.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author clw
 * @since 2019-11-01
 */
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements ISysPermissionService {

    @Autowired
    private ISysRolePermissionRelationService sysRolePermissionRelationService;

    @Override
    public CommonResult permissionList() {
        return CommonResult.success(baseMapper.selectPermissionList(0L));
    }

    @Override
    public List<String> getPermissionList(Long roleId) {
        List<String> sysPermissions = new ArrayList<>();
        if (roleId == null){
            return sysPermissions;
        }
        return baseMapper.selectByPermissions(roleId);
    }

    @Override
    public CommonResult selectMenu() {
        QueryWrapper<SysPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysPermission::getType,1);
        return CommonResult.success(list(queryWrapper));
    }

    @Override
    public List<Tree> permissionTree(Long pId) {
        return baseMapper.selectPermissionTree(pId);
    }

    @Override
    public void add(SysPermission sysPermission) {
        sysPermission.setAddTime(LocalDateTime.now());
        sysPermission.setUpdateTime(sysPermission.getAddTime());
        save(sysPermission);
    }

    @Override
    @Transactional
    public CommonResult deleted(Long id) {
        QueryWrapper<SysPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysPermission::getPId,id);
        int count = count(queryWrapper);
        if (count > 0) {
            return CommonResult.failed("还有下级，不能删除");
        }
        removeById(id);
        sysRolePermissionRelationService.lambdaUpdate().eq(SysRolePermissionRelation::getPermissionId,id).remove();
        return CommonResult.success();
    }

    @Override
    public CommonResult edit(SysPermission sysPermission) {
        if (sysPermission.getId().equals(sysPermission.getPId())){
            return CommonResult.failed("用户上级不能是自己");
        }
        if (sysPermission.getPId() != 0){
            //校验上级是否重复
            List<Long> ids = new ArrayList<>();
            ids.add(sysPermission.getId());
            if (checkSuperior(sysPermission.getPId(),ids)){
                return CommonResult.failed("不能选择这个为上级");
            }
        }
        sysPermission.setUpdateTime(LocalDateTime.now());
        updateById(sysPermission);
        return CommonResult.success();
    }

    /**
     * 校验用户上级,判断用户是否存在死循环情况,true代表有，false代表没有
     */
    private boolean checkSuperior(Long userId,List<Long> idList){
        boolean bool = false;
        if (userId == 0){
            return false;
        }
        if (idList.contains(userId)){
            return true;
        }
        idList.add(userId);
        SysPermission byId = getById(userId);
        bool = checkSuperior(byId.getPId(),idList);
        return bool;
    }
}
